const fs = require('fs');
const path = require('path');

// 使用HTMLCSStoImage服务的示例
async function renderWithOnlineService() {
    try {
        const htmlContent = fs.readFileSync(path.resolve(__dirname, '../deepseek_html_20250605_fb6f8f.html'), 'utf8');
        
        // 提取样式和内容
        const cardContent = `
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <script src="https://cdn.tailwindcss.com"></script>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    ${extractStyles(htmlContent)}
</head>
<body style="margin: 0; padding: 20px; background: linear-gradient(135deg, #1a1a2e 0%, #16213e 100%);">
    ${extractCardContent(htmlContent)}
</body>
</html>`;

        console.log('HTML内容已准备，您可以使用以下在线服务：');
        console.log('1. HTMLCSStoImage: https://htmlcsstoimage.com/');
        console.log('2. URL to Image: https://urltoimage.com/');
        console.log('3. HTML/CSS to Image API: https://docs.htmlcsstoimage.com/');
        
        // 保存HTML内容到文件，便于上传
        const outputPath = path.resolve(__dirname, 'card_for_online_service.html');
        fs.writeFileSync(outputPath, cardContent);
        console.log(`\nHTML文件已保存到: ${outputPath}`);
        console.log('您可以将此文件上传到在线服务或使用其API。');

    } catch (error) {
        console.error('处理HTML文件时出错:', error);
    }
}

function extractStyles(htmlContent) {
    const styleMatch = htmlContent.match(/<style[^>]*>([\s\S]*?)<\/style>/i);
    return styleMatch ? `<style>${styleMatch[1]}</style>` : '';
}

function extractCardContent(htmlContent) {
    const cardMatch = htmlContent.match(/<div[^>]*id="business-card"[^>]*>([\s\S]*?)<\/div>\s*<script/i);
    return cardMatch ? `<div class="card-container" id="business-card">${cardMatch[1]}</div>` : '';
}

renderWithOnlineService();